//
//  WZPTopView.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/14.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPTopView.h"
#import "WZPThemeItem.h"
#import <UIImageView+WebCache.h>
#import "NSDate+WZPDate.h"

@interface WZPTopView()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameText;
@property (weak, nonatomic) IBOutlet UILabel *timerText;
@property (weak, nonatomic) IBOutlet UILabel *text;

@end

@implementation WZPTopView
- (IBAction)clickBtn:(id)sender {
    // 弹出alert 控制器
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"收藏"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      NSLog(@"点击收藏");
    }];
    [alert addAction:action];

    //获取控制器(想快速获取的话,就使用根控制器)
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    
}

-(void)setItem:(WZPThemeItem *)item{

    [super setItem:item];
    
    // 头像
    [_iconView sd_setImageWithURL:[NSURL URLWithString:item.profile_image]];
    // 名字
    _nameText.text = item.screen_name;
    _timerText.text = [self timeStr];
    _text.text = item.text;
    
}

-(NSString *)timeStr{
    
    NSString * timeStr = self.item.created_at;
    // 获取日期格式化对象 字符转NsDate  NsDate转字符
    NSDateFormatter * fmt = [[NSDateFormatter alloc]init];
    // 设置时间格式
    fmt.dateFormat = @"yyy-MM-dd HH:mm:ss";
    // 获取发布时间
    NSDate * releaseDate = [fmt dateFromString:timeStr];
    // 获取与当前时间的差值
    NSDateComponents * com = [releaseDate dataWithNow];
    // 判断时间 是什么时候
    if ([releaseDate isThisyear]) {
        // 是不是今天
        if ([releaseDate isToday]) {
            if (com.hour > 1) {// 几个小时前
                timeStr = [NSString stringWithFormat:@"%ld小时前",com.hour];
           }else if (com.minute > 1){// 几分钟前
                timeStr = [NSString stringWithFormat:@"%ld分钟前",com.minute];
        
           }else{ //刚刚
               timeStr = @"刚刚";
           }
        }else if ([releaseDate isYesterday])
        {
            fmt.dateFormat = @"昨天 HH : mm";
            timeStr = [fmt stringFromDate:releaseDate];
            
        }else{
            fmt.dateFormat = @"MM-dd HH : mm";
            timeStr = [fmt stringFromDate:releaseDate];
        }
    }
    return timeStr;
}
@end
